// VideoConvert.cpp : implementation file
//

#include "stdafx.h"
#include "manualCalib.h"
#include "VideoConvert.h"
#include "cvxVideo.h"



// CVideoConvert dialog

IMPLEMENT_DYNAMIC(CVideoConvert, CDialog)

CVideoConvert::CVideoConvert(CWnd* pParent /*=NULL*/)
	: CDialog(CVideoConvert::IDD, pParent)
	, m_convertWidth(0)
	, m_convertHeight(0)
{
	//int test = 1;
}

CVideoConvert::~CVideoConvert()
{
}

//added
void CVideoConvert::SetConvert(const string &leftFile, const string &rightFile, 
							 const SIZE &leftRes, const SIZE &rightRes)
{
	m_leftFileName = leftFile;
	m_rightFileName = rightFile;
	m_leftSize  = leftRes;
	m_rightSize = rightRes;
}

void CVideoConvert::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LEFT_RESOLUTION, m_leftRes);
	DDX_Control(pDX, IDC_RIGHT_RESOLUTION, m_rightRes);
	DDX_Control(pDX, IDC_LEFT_START_TIME, m_leftStartTime);
	DDX_Control(pDX, IDC_LEFT_END_TIME, m_leftEndTime);
	DDX_Control(pDX, IDC_RIGHT_START_TIME, m_rightStartTime);
	DDX_Control(pDX, IDC_RIGHT_END_TIME, m_rightEndTime);
	DDX_Text(pDX, IDC_CONVERT_WIDTH, m_convertWidth);
	DDX_Text(pDX, IDC_CONVERT_HEIGHT, m_convertHeight);
	DDV_MinMaxInt(pDX, m_convertHeight, 0, 1920);
	DDX_Control(pDX, IDC_CONVERT_START_TIME, m_convertStartTime);
	DDX_Control(pDX, IDC_CONVERT_END_TIME, m_convertEndTime);
	DDX_Control(pDX, IDC_JUST_LEFT, m_justLeftTotal);
	DDX_Control(pDX, IDC_JUST_RIGHT, m_justRightTotal);
}


BEGIN_MESSAGE_MAP(CVideoConvert, CDialog)
	ON_EN_CHANGE(IDC_LEFT_RESOLUTION, &CVideoConvert::OnEnChangeLeftResolution)
	ON_BN_CLICKED(IDC_VIDEO_CONVERT_START, &CVideoConvert::OnBnClickedVideoConvertStart)
	ON_BN_CLICKED(IDC_LOAD_TIME_CODE, &CVideoConvert::OnBnClickedLoadTimeCode)
END_MESSAGE_MAP()


// CVideoConvert message handlers

void CVideoConvert::OnEnChangeLeftResolution()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
}

BOOL CVideoConvert::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here

	//show left and right resolution
	char bufL[255] = {NULL};
	char bufR[255] = {NULL};
	sprintf(bufL, "%d X %d", m_leftSize.cx, m_leftSize.cy);
	sprintf(bufR, "%d X %d", m_rightSize.cx, m_rightSize.cy);
	

	CEdit *pEdit = NULL;
	pEdit = (CEdit*)GetDlgItem(IDC_LEFT_RESOLUTION);
	ASSERT(pEdit);
	pEdit->SetWindowText(bufL);  //m_leftRes

	pEdit = (CEdit*)GetDlgItem(IDC_RIGHT_RESOLUTION);
	ASSERT(pEdit);
	pEdit->SetWindowText(bufR);

	//time code
	CIPAddressCtrl *pAdd = NULL;
	pAdd = (CIPAddressCtrl*)GetDlgItem(IDC_LEFT_START_TIME);
	ASSERT(pAdd);
	pAdd->SetAddress(0, 0, 0, 0);
	pAdd = (CIPAddressCtrl*)GetDlgItem(IDC_LEFT_END_TIME);
	ASSERT(pAdd);
	pAdd->SetAddress(0, 0, 0, 0);

	pAdd = (CIPAddressCtrl*)GetDlgItem(IDC_RIGHT_START_TIME);
	ASSERT(pAdd);
	pAdd->SetAddress(0, 0, 0, 0);
	pAdd = (CIPAddressCtrl*)GetDlgItem(IDC_RIGHT_END_TIME);
	ASSERT(pAdd);
	pAdd->SetAddress(0, 0, 0, 0);

	m_convertWidth = 1920;  //@todo, it not work
	m_convertHeight = 1080;
	

	//set for test
	m_leftStartTime.SetAddress(15, 33, 22, 23);
	m_leftEndTime.SetAddress(15, 34, 14, 18);

	m_rightStartTime.SetAddress(15, 33, 22, 23);
	m_rightEndTime.SetAddress(15, 34, 14, 18);

	m_convertStartTime.SetAddress(15, 33, 30, 27);
	m_convertEndTime.SetAddress(15, 34, 13, 18);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CVideoConvert::OnBnClickedVideoConvertStart()
{
	// TODO: Add your control notification handler code here
	
	UpdateData();

	if (m_justLeftTotal.GetCheck() == BST_CHECKED)
	{
		string saveFileLeft;
		CFileDialog dlgFileL(FALSE, "avi", "left.avi", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "AVI (*.avi)", NULL);
		if (dlgFileL.DoModal() == IDOK)
		{
			saveFileLeft = dlgFileL.GetPathName();		
		}
		if (!saveFileLeft.empty())
		{
			CvxVideo::writeToAVI(m_leftFileName.c_str(), saveFileLeft.c_str(), 24, 
								 cvSize(m_convertWidth, m_convertHeight), 0, 10000, -2);			
		}
		AfxMessageBox(_T("left video convert complete"), MB_OK);
		
	}
	else if (m_justRightTotal.GetCheck() == BST_CHECKED)
	{
		string saveFileRight;
		CFileDialog dlgFileR(FALSE, "avi", "right.avi", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "AVI (*.avi)", NULL);
		if (dlgFileR.DoModal() == IDOK)
		{
			saveFileRight = dlgFileR.GetPathName();
		}

		if (!saveFileRight.empty())
		{
			CvxVideo::writeToAVI(m_rightFileName.c_str(), saveFileRight.c_str(), 24, cvSize(m_convertWidth, m_convertHeight),
								 0, 100000, -2);			
		}
		AfxMessageBox(_T("Rright video convert complete"), MB_OK);
	}
	else
	{
		//get time code
		BYTE leftTime[2][4]    = {0};
		BYTE rightTime[2][4]   = {0};
		BYTE convertTime[2][4] = {0};
		m_leftStartTime.GetAddress(leftTime[0][0], leftTime[0][1], leftTime[0][2], leftTime[0][3]);
		m_leftEndTime.GetAddress(leftTime[1][0], leftTime[1][1], leftTime[1][2], leftTime[1][3]);

		m_rightStartTime.GetAddress(rightTime[0][0], rightTime[0][1], rightTime[0][2], rightTime[0][3]);
		m_rightEndTime.GetAddress(rightTime[1][0], rightTime[1][1], rightTime[1][2], rightTime[1][3]);

		m_convertStartTime.GetAddress(convertTime[0][0], convertTime[0][1], convertTime[0][2], convertTime[0][3]);
		m_convertEndTime.GetAddress(convertTime[1][0], convertTime[1][1], convertTime[1][2], convertTime[1][3]);

		int leftStartFrame = leftTime[0][0]*(60*60*30) + leftTime[0][1]*(60*30) + leftTime[0][2] *(30) + leftTime[0][3];
		int leftEndFrame = leftTime[1][0]*(60*60*30) + leftTime[1][1]*(60*30) + leftTime[1][2] *(30) + leftTime[1][3];
		ASSERT(leftEndFrame >= leftStartFrame);

		int rightStartFrame = rightTime[0][0]*(60*60*30) + rightTime[0][1]*(60*30) + rightTime[0][2] *(30) + rightTime[0][3];
		int rightEndFrame   = rightTime[1][0]*(60*60*30) + rightTime[1][1]*(60*30) + rightTime[1][2] *(30) + rightTime[1][3];
		ASSERT(rightEndFrame >= rightStartFrame);

		//computer start frame and frame length
		int convertStartFrame = convertTime[0][0]*(60*60*30) + convertTime[0][1]*(60*30) + convertTime[0][2] *(30) + convertTime[0][3];
		int convertEndFrame   = convertTime[1][0]*(60*60*30) + convertTime[1][1]*(60*30) + convertTime[1][2] *(30) + convertTime[1][3];
		ASSERT(convertEndFrame > convertStartFrame);
		ASSERT(convertStartFrame >= leftStartFrame && convertStartFrame >= rightStartFrame);
		ASSERT(convertEndFrame <= leftEndFrame && convertEndFrame <= rightEndFrame);
		ASSERT(m_convertWidth > 0 && m_convertHeight > 0);

		int leftJumpFrame  = convertStartFrame - leftStartFrame;
		int rightJumpFrame = convertStartFrame - rightStartFrame;

		int frameLength = convertEndFrame - convertStartFrame;

		string saveFileLeft;
		string saveFileRight;

		//get save file name
		CFileDialog dlgFileL(FALSE, "avi", "left.avi", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "AVI (*.avi)", NULL);
		if (dlgFileL.DoModal() == IDOK)
		{
			saveFileLeft = dlgFileL.GetPathName();		
		}
		
		CFileDialog dlgFileR(FALSE, "avi", "right.avi", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "AVI (*.avi)", NULL);
		if (dlgFileR.DoModal() == IDOK)
		{
			saveFileRight = dlgFileR.GetPathName();
		}

		//save left and right video

		if (!saveFileLeft.empty())
		{
			CvxVideo::writeToAVI(m_leftFileName.c_str(), saveFileLeft.c_str(), 24, cvSize(m_convertWidth, m_convertHeight),
				leftJumpFrame, frameLength, -2);
			int test = 1;
		}
		if (!saveFileRight.empty())
		{
			CvxVideo::writeToAVI(m_rightFileName.c_str(), saveFileRight.c_str(), 24, cvSize(m_convertWidth, m_convertHeight),
				rightJumpFrame, frameLength, -2);
			int test = 1;
		}		
	}
	
}

void CVideoConvert::OnBnClickedLoadTimeCode()
{
	// TODO: Add your control notification handler code here
	// load single file from a dialog
	CFileDialog iFileDlg(true);
	string strFile;

	if (iFileDlg.DoModal()==IDOK)
	{		
		strFile=iFileDlg.GetPathName();
		FILE *pf = fopen(strFile.c_str(), "r");
		if (pf)
		{
			//@todo input check
			int a, b, c, d;
			fscanf(pf, "%d %d %d %d", &a, &b, &c, &d);
			m_leftStartTime.SetAddress(a, b, c, d);
			fscanf(pf, "%d %d %d %d", &a, &b, &c, &d);
			m_leftEndTime.SetAddress(a, b, c, d);
			fscanf(pf, "%d %d %d %d", &a, &b, &c, &d);
			m_rightStartTime.SetAddress(a, b, c, d);
			fscanf(pf, "%d %d %d %d", &a, &b, &c, &d);
			m_rightEndTime.SetAddress(a, b, c, d);
			fscanf(pf, "%d %d %d %d", &a, &b, &c, &d);
			m_convertStartTime.SetAddress(a, b, c, d);
			fscanf(pf, "%d %d %d %d", &a, &b, &c, &d);
			m_convertEndTime.SetAddress(a, b, c, d);
			InvalidateRect(0, FALSE);
			fclose(pf);
		}
	}
}
